package run;
import initBd.InitBD;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

import parse.Parse;
import parse.ParseSplitLignes;
import connexionBd.ConnexionBd;


public class Main {
	private static java.sql.Connection connexion = ConnexionBd.getConnection();
	private static String table_maison = "CREATE TABLE `energie`.`maison` ( `id_maison` INT NOT NULL,  PRIMARY KEY (`id_maison`));";
	private static String table_equipement= "  CREATE TABLE `energie`.`equipement` ( `id_equipement` INT NOT NULL, "
			+ "`libelle_equipement` VARCHAR(45) NULL, "
			+ "`infos_equipement` VARCHAR(45) NULL, "
			+ "PRIMARY KEY (`id_equipement`));";
	private static String table_releve = ""
			+ "  CREATE TABLE `energie`.`releve` ( `id_Maison` INT NOT NULL, "
			+ "`id_Equipement` VARCHAR(45) NOT NULL,  "
			+ "`date_Heure` DATETIME NOT NULL,  `state` INT NULL DEFAULT 0,  "
			+ "`mesure_energie` INT NULL DEFAULT 0,  "
			+ "PRIMARY KEY (`id_Maison`, `id_Equipement`, `date_Heure`));";
	
	 public static void main(String[] args) throws SQLException {
		   Scanner sc = new Scanner(System.in);
		   boolean arret = false;
		 while(!arret){
			   System.out.println("############################## ");
			   System.out.println("Veuillez choisir une action ");
			   System.out.println("1- Drop Table ");
			   System.out.println("2- Create Table ");
			   System.out.println("3- Parser Fichier");
			   System.out.println("0- Exit ");
			   System.out.println("############################## ");
			   String str = sc.nextLine();
		switch (str) {
		case "0":
			arret = true;
			System.err.println("Allez Salut !!!");
			connexion.close();
			break;
		case "1":
			InitBD.dropTable("equipement", connexion);
			InitBD.dropTable("maison", connexion);
			InitBD.dropTable("releve", connexion);
			System.out.println("Drop table OK!");
			break;
		case "2":
			InitBD.createTable(table_equipement, connexion);
			InitBD.createTable(table_maison, connexion);
			InitBD.createTable(table_releve, connexion);
			System.out.println("Creation table OK !");
			break;
		case "3":
			System.out.println("Combien de fichier ? ");
			 String nbFichier = sc.nextLine();	
			 double startTime = System.currentTimeMillis();
			parseEtInsertEnBase(Integer.parseInt(nbFichier));
			double estimatedTime = (System.currentTimeMillis() - startTime) / 1000;
			System.out.println("Peuplement table OK !");
			System.out.println("Durée : " + estimatedTime + " sec");
			break;
		default:
			System.err.println("Choix non valide ");
			break;
		   
		  }
	
	
		 }
	 }

	 private static void parseEtInsertEnBase(int nbFichier) throws SQLException {
		 File directory = new File("files");
		 Statement stm = connexion.createStatement();
		 int cpt= 0, stmSplit = 20;
		 // returns pathnames for files and directory
		 File[] paths = directory.listFiles();
		 // for each pathname in pathname array
		 for(File path:paths)
		 {
			 if(cpt < nbFichier){
				 try {
					 System.out.println("-------"+ path.getName() + "-------");
//					 Parse.parseContenu(path, stm);
					 ParseSplitLignes.parseContenu(path, stm);
				 } catch (IOException e) {
					 e.printStackTrace();
				 } catch (SQLException e) {
					 //conn.rollback();
					 System.out.println("Erreur SQL ! ");
					 e.printStackTrace();
				 }
				 cpt++;
			 }
			 
			 if (cpt % stmSplit == 0) {
				 stm.executeBatch();
				 connexion.commit();
				 stm = connexion.createStatement();
			 }
		 }
		 
		 int res[] = stm.executeBatch();
		 connexion.commit();
	 }
}
